package gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
// top level widget package com.google.gwt.user.client.ui
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import gwt.client.events.NavigationChangeEvent;
import gwt.client.interfaces.NavigationChangeListener;
import gwt.client.rpc.MFService;
import gwt.client.rpc.MFServiceAsync;

public class MFClient implements EntryPoint, NavigationChangeListener{

    private Widget currentPanel;
    private DockLayoutPanel mainPanel;

    public void onModuleLoad() {

        mainPanel = new DockLayoutPanel(Style.Unit.PX);
        mainPanel.getElement().getStyle().setProperty("border","red solid 2px");

        NavigationPanel navigationPanel = new NavigationPanel();
        navigationPanel.addNavigationChangeListener(this);

        HeaderPanel headerPanel = new HeaderPanel();
        FooterPanel footerPanel = new FooterPanel();

        currentPanel = new RepositoryPanel();

        mainPanel.addNorth(headerPanel, 50); // north & south first to fil width
        mainPanel.addSouth(footerPanel, 50);
        mainPanel.addWest(navigationPanel, 200); // width of container for navigationPanel
        mainPanel.add(currentPanel); // add last to fill space

        mainPanel.setWidth("800px");
        mainPanel.setHeight("490px");

        // should be RootLayoutPanel (pg 128), but keeping for x, y params
        RootPanel.get().add(mainPanel, 20, 20);

        doRequest();
    }

    private void doRequest() {

        GWT.log("MFClient.doRequest()");
        //Window.alert("MFClient.doRequest()");

        MFServiceAsync mfService = (MFServiceAsync) GWT.create(MFService.class);

        AsyncCallback callback = new AsyncCallback() {

            public void onFailure(Throwable caught) {
                GWT.log("AsyncCallback.onFailure");
            }

            @Override
            public void onSuccess(Object result) {
                GWT.log("AsyncCallback.onSuccess "+result.toString());
            }
        };

        StringBuilder strReq = new StringBuilder();

        strReq.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"+"\n");
        strReq.append("<MFRequest>"+"\n");
        strReq.append("<Request type='Repositories' data=''/>"+"\n");
        strReq.append("</MFRequest>"+"\n");

        mfService.getRepositories(strReq.toString(), callback);

    }

    @Override
    public void onNavigationChangeEvent(NavigationChangeEvent event) {

        String btnRef = event.getBtnRef();

        Widget newPanel = null;

        if(btnRef.equals("Repositories")){
            newPanel = new RepositoryPanel();
        }else if(btnRef.equals("Download")) {
            newPanel = new DownloadPanel();
        }else if(btnRef.equals("Upload")) {
            newPanel = new UploadPanel();
        }else if(btnRef.equals("Settings")) {
            newPanel = new SettingsPanel();
        }else if(btnRef.equals("Help")) {
            newPanel = new HelpPanel();
        }else if(btnRef.equals("Reporting")) {
            newPanel = new ReportsPanel();
        }

        if(newPanel != null) {
            mainPanel.remove(currentPanel);
            currentPanel = newPanel;
            mainPanel.add(currentPanel);
        }
    }
}
